package com.baobao.web;

import com.baobao.dao.BanjiDAO;

import com.baobao.domain.Banji;

import com.baobao.service.BanjiService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.WebDataBinder;

import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import org.springframework.web.servlet.ModelAndView;

/**
 * Spring MVC controller that handles CRUD requests for Banji entities
 * 
 */

@Controller("BanjiController")
public class BanjiController {

	/**
	 * DAO injected by Spring that manages Banji entities
	 * 
	 */
	@Autowired
	private BanjiDAO banjiDAO;

	/**
	 * Service injected by Spring that provides CRUD operations for Banji entities
	 * 
	 */
	@Autowired
	private BanjiService banjiService;

	/**
	 * Show all Banji entities
	 * 
	 */
	@RequestMapping("/indexBanji")
	public ModelAndView listBanjis() {
		ModelAndView mav = new ModelAndView();

		mav.addObject("banjis", banjiService.loadBanjis());

		mav.setViewName("banji/listBanjis.jsp");

		return mav;
	}

	/**
	 */
	@RequestMapping("/banjiController/binary.action")
	public ModelAndView streamBinary(@ModelAttribute HttpServletRequest request, @ModelAttribute HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("streamedBinaryContentView");
		return mav;

	}

	/**
	 * Entry point to show all Banji entities
	 * 
	 */
	public String indexBanji() {
		return "redirect:/indexBanji";
	}

	/**
	 * Select an existing Banji entity
	 * 
	 */
	@RequestMapping("/selectBanji")
	public ModelAndView selectBanji(@RequestParam String idKey) {
		ModelAndView mav = new ModelAndView();

		mav.addObject("banji", banjiDAO.findBanjiByPrimaryKey(idKey));
		mav.setViewName("banji/viewBanji.jsp");

		return mav;
	}

	/**
	 * Save an existing Banji entity
	 * 
	 */
	@RequestMapping("/saveBanji")
	public String saveBanji(@ModelAttribute Banji banji) {
		banjiService.saveBanji(banji);
		return "forward:/indexBanji";
	}

	/**
	 * Delete an existing Banji entity
	 * 
	 */
	@RequestMapping("/deleteBanji")
	public String deleteBanji(@RequestParam String idKey) {
		Banji banji = banjiDAO.findBanjiByPrimaryKey(idKey);
		banjiService.deleteBanji(banji);
		return "forward:/indexBanji";
	}

	/**
	 * Register custom, context-specific property editors
	 * 
	 */
	@InitBinder
	public void initBinder(WebDataBinder binder, HttpServletRequest request) { // Register static property editors.
		binder.registerCustomEditor(java.util.Calendar.class, new org.skyway.spring.util.databinding.CustomCalendarEditor());
		binder.registerCustomEditor(byte[].class, new org.springframework.web.multipart.support.ByteArrayMultipartFileEditor());
		binder.registerCustomEditor(boolean.class, new org.skyway.spring.util.databinding.EnhancedBooleanEditor(false));
		binder.registerCustomEditor(Boolean.class, new org.skyway.spring.util.databinding.EnhancedBooleanEditor(true));
		binder.registerCustomEditor(java.math.BigDecimal.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(java.math.BigDecimal.class, true));
		binder.registerCustomEditor(Integer.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Integer.class, true));
		binder.registerCustomEditor(java.util.Date.class, new org.skyway.spring.util.databinding.CustomDateEditor());
		binder.registerCustomEditor(String.class, new org.skyway.spring.util.databinding.StringEditor());
		binder.registerCustomEditor(Long.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Long.class, true));
		binder.registerCustomEditor(Double.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Double.class, true));
	}

	/**
	 * Select the Banji entity for display allowing the user to confirm that they would like to delete the entity
	 * 
	 */
	@RequestMapping("/confirmDeleteBanji")
	public ModelAndView confirmDeleteBanji(@RequestParam String idKey) {
		ModelAndView mav = new ModelAndView();

		mav.addObject("banji", banjiDAO.findBanjiByPrimaryKey(idKey));
		mav.setViewName("banji/deleteBanji.jsp");

		return mav;
	}

	/**
	 * Create a new Banji entity
	 * 
	 */
	@RequestMapping("/newBanji")
	public ModelAndView newBanji() {
		ModelAndView mav = new ModelAndView();

		mav.addObject("banji", new Banji());
		mav.addObject("newFlag", true);
		mav.setViewName("banji/editBanji.jsp");

		return mav;
	}

	/**
	 * Edit an existing Banji entity
	 * 
	 */
	@RequestMapping("/editBanji")
	public ModelAndView editBanji(@RequestParam String idKey) {
		ModelAndView mav = new ModelAndView();

		mav.addObject("banji", banjiDAO.findBanjiByPrimaryKey(idKey));
		mav.setViewName("banji/editBanji.jsp");

		return mav;
	}
}